$(function(){//随机验证码
    var arrs1=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",1,2,3,4,5,6,7,8,9,0]
    $("button:eq(0)").click(function(){
        // console.log(Math.floor(Math.random()*62));//产生0-61的索引
        $("button:eq(0)").html(arrs1[Math.floor(Math.random()*62)]+arrs1[Math.floor(Math.random()*62)]+arrs1[Math.floor(Math.random()*62)]+arrs1[Math.floor(Math.random()*62)]);
    });
});

var users=JSON.parse(localStorage.getItem("users"));
var usernames=[];
for(var i=0;i<users.length;i++){
    usernames.push(users[i].username);
}
console.log(usernames);
$(function(){
    layui.use('form',function(){
        var form=layui.form;
        form.verify({
            username:function(value){
                if($("input[name=username]").val()==""){
                    return "请输入用户名"
                }
                if(usernames.indexOf($("input[name=username]").val())!=-1){
                    return "用户名已存在"
                }
            },
            password:function(value){
                if($("input[name=password]").val()==""){
                    return "请输入密码"
                }
            },
            rePassword:function(value){
                if($("input[name=rePassword]").val()==""){
                    return "请输入确认密码"
                }
                if($("input[name=password]").val()!=value){
                    return "两次密码输入不一致"
                }
            },
            yzm:function(value){
                if($("button:eq(0)").html()!=value){
                    return "请输入正确的验证码"
                }
            }
        });
        //监听提交
        form.on('submit(formDemo)',function(data){
            // console.log(JSON.stringify(data.field));
            var username=$("input:eq(0)").val();
            var password=$("input:eq(1)").val();
            var phone=$("input:eq(3)").val();
            var users=JSON.parse(localStorage.getItem("users"));
            console.log(users.length);
            var userNew={
                "id":users.length+1,
                "username":username,
                "password":password,
                "phone":phone
            };
            users.push(userNew);
            // console.log(users);
            localStorage.setItem("users",JSON.stringify(users));
            location.href="登录界面.html";
            return false;
        });
    });
});